#include "serverPool.h"
typedef struct trains_s {
    int length;
    char data[1000];
} train_t;
//小火车+进度条
int transFile(int netfd){
   train_t train;
   char filename[] = "file1";
   train.length = 5;//放火车头
   memcpy(train.data,filename,train.length);//放车厢
   send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);//发火车头
   send(netfd,train.data,train.length,MSG_NOSIGNAL);//发车厢
   int fd = open(filename,O_RDWR);

   struct stat statbuf;
   fstat(fd,&statbuf);
   off_t filesize = statbuf.st_size;
   printf("filesize = %ld\n", filesize);
   train.length = sizeof(filesize);
   memcpy(train.data,&filesize,train.length);
   send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);//发火车头
   send(netfd,train.data,train.length,MSG_NOSIGNAL);//发车厢

   
   char *p = (char *)mmap(NULL,filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
   
   off_t offset = 0; 
   while(offset < filesize){
       // 判断本次要发送的内容长度
       if(filesize - offset > 1000){
           train.length = 1000;
       }
       else{
           train.length = filesize - offset;
       }
       // 先发头
       send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
       send(netfd,p+offset,train.length,MSG_NOSIGNAL);
       offset += train.length;
   }
   train.length = 0;
   send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
   munmap(p,filesize);

   close(fd);
   return 0;
}
